package com.oa.log.controller;

import java.io.IOException;
import java.io.OutputStream;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.oa.entity.ExtPageable;
import com.oa.log.annotation.Log;
import com.oa.log.dto.LogEntityDto;
import com.oa.log.service.LogManagerService;

@Controller
@RequestMapping("/log")
public class LogManagerController {
	@Resource
	private LogManagerService logManagerService;

	// log/searchLog 查询日志
	@GetMapping("/searchLog")
	public @ResponseBody Page<LogEntityDto> searchLog(LogEntityDto logDto, ExtPageable pageable) {
		return logManagerService.findAllLog(logDto, pageable.getPageable());
	}

	// log/export 导出日志
	@GetMapping("/export")
	@Log(oprate = "导出日志")
	@PreAuthorize("hasAnyAuthority('ADMIN','HIGH_MANAGER')")
	public void export(HttpServletRequest request, HttpServletResponse response) {
		try {
			HSSFWorkbook wb = logManagerService.export();
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-disposition", "attachment;filename=log.xls");

			OutputStream ouputStream = response.getOutputStream();
			wb.write(ouputStream);
			ouputStream.flush();
			ouputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}
}
